{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第8章 numpy 数值计算基础\n",
    "\n",
    "### 8.1 掌握 numpy 数组对象 ndarray\n",
    "* 8.1.1 创建数组对象\n",
    "* 8.1.2 生成随机数\n",
    "* 8.1.3 通过索引访问数组\n",
    "* 8.1.4 变换数组的形态\n",
    "\n",
    "### 8.2 掌握 numpy 矩阵与通用函数\n",
    "* 8.2.1 numpy 的矩阵\n",
    "* 8.2.2 numpy 的通用函数\n",
    "\n",
    "### 8.3 利用 numpy 进行统计分析\n",
    "* 8.3.1 numpy 文件存取\n",
    "* 8.3.2 运用 numpy 进行统计分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一步 导入 numpy 模块，并重命名为np\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.1.1 创建数组对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 用 numpy.array() 创建ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 383,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为： [1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np  #导入NumPy库\n",
    "arr1 = np.array([1, 2, 3, 4])  #创建一维数组\n",
    "print('创建的数组为：',arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的数组为：\n",
      " [[ 1  2  3  4]\n",
      " [ 4  5  6  7]\n",
      " [ 7  8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = np.array([[1, 2, 3, 4],[4, 5, 6, 7], [7, 8, 9, 10]])    #创建二维数组\n",
    "print('创建的数组为：\\n',arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 513,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 维度\n",
    "arr = np.array([[[1,2,3],[4,5,6]]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 514,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[1, 2, 3],\n",
       "        [4, 5, 6]]])"
      ]
     },
     "execution_count": 514,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 517,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 517,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.ndim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 519,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2, 3)"
      ]
     },
     "execution_count": 519,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 层层抽丝剥茧\n",
    "arr.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 524,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[[1, 2, 3], [4, 5, 6]]]"
      ]
     },
     "execution_count": 524,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[[1, 2, 3],[4, 5, 6]]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 525,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([1, 2, 3], [4, 5, 6])"
      ]
     },
     "execution_count": 525,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1, 2, 3],[4, 5, 6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 526,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 526,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[1, 2, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 查看ndarray的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 528,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组形状为： (4, 3)\n"
     ]
    }
   ],
   "source": [
    "print('数组形状为：',arr2.shape)  #查看数组结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 450,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度为： int32\n"
     ]
    }
   ],
   "source": [
    "print('数组维度为：',arr2.dtype)  #查看数组类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 451,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组元素个数为： 12\n"
     ]
    }
   ],
   "source": [
    "print('数组元素个数为：',arr2.size)  #查看数组元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 452,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组每个元素大小为： 4\n"
     ]
    }
   ],
   "source": [
    "print('数组每个元素大小为：',arr2.itemsize)  #查看数组每个元素大小"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 改变ndarray的形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 454,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新设置 shape 后的 arr2 为： [[ 1  2]\n",
      " [ 3  4]\n",
      " [ 4  5]\n",
      " [ 6  7]\n",
      " [ 7  8]\n",
      " [ 9 10]]\n"
     ]
    }
   ],
   "source": [
    "arr2 = arr2.reshape(6,2) # 用reshape重新设置 形状\n",
    "print('重新设置 shape 后的 arr2 为：',arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 455,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重新设置 shape 后的 arr2 为： [[ 1  2  3]\n",
      " [ 4  4  5]\n",
      " [ 6  7  7]\n",
      " [ 8  9 10]]\n"
     ]
    }
   ],
   "source": [
    "arr2.shape = 4,3 #重新设置 shape\n",
    "print('重新设置 shape 后的 arr2 为：',arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 改变（指定）数组元素的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 465,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([[1,2,3],[4,5,6]],dtype = 'int32')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 467,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 467,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 466,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 466,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 457,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4,  4,  5],\n",
       "       [ 6,  7,  7],\n",
       "       [ 8,  9, 10]])"
      ]
     },
     "execution_count": 457,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 458,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 458,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 459,
   "metadata": {},
   "outputs": [],
   "source": [
    "arr2 = np.float64(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 460,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  2.,  3.],\n",
       "       [ 4.,  4.,  5.],\n",
       "       [ 6.,  7.,  7.],\n",
       "       [ 8.,  9., 10.]])"
      ]
     },
     "execution_count": 460,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 461,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 461,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 用numpy的函数创建特殊的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 414,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用 arange 函数创建的数组为：\n",
      " [0.  0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]\n"
     ]
    }
   ],
   "source": [
    "print('使用 arange 函数创建的数组为：\\n',np.arange(0,1,0.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 415,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用 linspace 函数创建的数组为： [0.         0.09090909 0.18181818 0.27272727 0.36363636 0.45454545\n",
      " 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.        ]\n"
     ]
    }
   ],
   "source": [
    "print('使用 linspace 函数创建的数组为：',np.linspace(0, 1, 12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 416,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用logspace函数创建的数组为： [  1.           1.27427499   1.62377674   2.06913808   2.6366509\n",
      "   3.35981829   4.2813324    5.45559478   6.95192796   8.8586679\n",
      "  11.28837892  14.38449888  18.32980711  23.35721469  29.76351442\n",
      "  37.92690191  48.32930239  61.58482111  78.47599704 100.        ]\n"
     ]
    }
   ],
   "source": [
    "print('使用logspace函数创建的数组为：',np.logspace(0, 2, 20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 395,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用 linspace 函数创建的数组为： [0.         0.09090909 0.18181818 0.27272727 0.36363636 0.45454545\n",
      " 0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1.        ]\n"
     ]
    }
   ],
   "source": [
    "print('使用 linspace 函数创建的数组为：',np.linspace(0, 1, 12))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 396,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用logspace函数创建的数组为： [  1.           1.27427499   1.62377674   2.06913808   2.6366509\n",
      "   3.35981829   4.2813324    5.45559478   6.95192796   8.8586679\n",
      "  11.28837892  14.38449888  18.32980711  23.35721469  29.76351442\n",
      "  37.92690191  48.32930239  61.58482111  78.47599704 100.        ]\n"
     ]
    }
   ],
   "source": [
    "print('使用logspace函数创建的数组为：',np.logspace(0, 2, 20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 397,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用zeros函数创建的数组为： [[0. 0. 0.]\n",
      " [0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "print('使用zeros函数创建的数组为：',np.zeros((2,3)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 398,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用eye函数创建的数组为： [[1. 0. 0.]\n",
      " [0. 1. 0.]\n",
      " [0. 0. 1.]]\n"
     ]
    }
   ],
   "source": [
    "print('使用eye函数创建的数组为：',np.eye(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 399,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用diag函数创建的数组为： [[1 0 0 0]\n",
      " [0 2 0 0]\n",
      " [0 0 3 0]\n",
      " [0 0 0 4]]\n"
     ]
    }
   ],
   "source": [
    "print('使用diag函数创建的数组为：',np.diag([1,2,3,4]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 400,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用ones函数创建的数组为： [[1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]\n",
      " [1. 1. 1.]]\n"
     ]
    }
   ],
   "source": [
    "print('使用ones函数创建的数组为：',np.ones((5,3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 自定义对象类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 468,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为： [('name', '<U40'), ('numitems', '<i8'), ('price', '<f8')]\n"
     ]
    }
   ],
   "source": [
    "df = np.dtype([(\"name\", np.str_, 40), (\"numitems\", np.int64), (\"price\",np.float64)])\n",
    "print('数据类型为：',df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 402,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据类型为： <U40\n",
      "数据类型为： <U40\n"
     ]
    }
   ],
   "source": [
    "print('数据类型为：',df[\"name\"])\n",
    "print('数据类型为：',np.dtype(df[\"name\"]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 403,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "自定义数据为： [('tomatoes', 42, 4.14) ('cabbages', 13, 1.72)]\n"
     ]
    }
   ],
   "source": [
    "itemz = np.array([(\"tomatoes\", 42, 4.14), (\"cabbages\", 13, 1.72)], dtype=df)\n",
    "print('自定义数据为：',itemz)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.1.2 生成随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 470,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的随机数组为： [0.06931465 0.15904753 0.69852975 0.26405636 0.93772314 0.00612981\n",
      " 0.65584557 0.84982297 0.9682767  0.53524401]\n"
     ]
    }
   ],
   "source": [
    "#  random  -- Return random floats in the half-open interval [0.0, 1.0)\n",
    "print('生成的随机数组为：',np.random.random(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 473,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的均匀分布的随机数组为：\n",
      " [[0.0201753  0.00625234 0.14159214 0.94772172]\n",
      " [0.83553607 0.8814474  0.52243416 0.6278006 ]\n",
      " [0.2601612  0.96163849 0.75119044 0.60968312]]\n"
     ]
    }
   ],
   "source": [
    "# rand  --- 均匀， Create an array of the given shape and populate it with  random samples from a uniform distribution  over ``[0, 1)``.\n",
    "print('生成的均匀分布的随机数组为：\\n',np.random.rand(3,4))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 512,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成服从正态分布的随机数组为：\n",
      " [[ 0.35753652 -2.34326191 -1.08483259  0.55969629]\n",
      " [ 0.93946935 -0.97848104  0.50309684  0.40641447]\n",
      " [ 0.32346101 -0.49341088 -0.79201679 -0.84236793]]\n"
     ]
    }
   ],
   "source": [
    "# randn  --- 标准正态，Return a sample (or samples) from the \"standard normal\" distribution.\n",
    "print('生成服从正态分布的随机数组为：\\n',np.random.randn(3,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 594,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成的有上下界限的随机整数数组为： [[6 6 6 3 4]\n",
      " [2 9 9 7 5]]\n"
     ]
    }
   ],
   "source": [
    "# randint  --- 整数，Return random integers from the \"discrete uniform\" distribution of the specified dtype \n",
    "# in the \"half-open\" interval [`low`, `high`). If `high` is None (the default), then results are from [0, `low`).\n",
    "print('生成的有上下界限的随机整数数组为：',np.random.randint(2,10,size = [2,5]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 485,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 4, 9],\n",
       "       [9, 6, 4],\n",
       "       [3, 6, 5],\n",
       "       [8, 9, 4]])"
      ]
     },
     "execution_count": 485,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 设定随机种子，让随机数可重现\n",
    "np.random.seed(20)\n",
    "np.random.randint(2,10,size=(4,3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.1.3 通过索引访问数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 一维数组的索引访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 489,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： 5\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(10)\n",
    "print('索引结果为：',arr[5])  #用整数作为下标可以获取数组中的某个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 490,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [3 4]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[3:5])  #用范围作为下标获取数组的一个切片，包括arr[3]不包括arr[5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 491,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [0 1 2 3 4]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[:5])  #省略开始下标，表示从arr[0]开始"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 492,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： 9\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[-1])  #下标可以使用负数，-1表示从数组后往前数的第一个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 493,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [  0   1 100 101   4   5   6   7   8   9]\n"
     ]
    }
   ],
   "source": [
    "arr[2:4] = 100,101\n",
    "print('索引结果为：',arr)  #下标还可以用来修改元素的值\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 494,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [  1 101   5   7]\n"
     ]
    }
   ],
   "source": [
    "#范围中的第三个参数表示步长，2表示隔一个元素取一个元素\n",
    "print('索引结果为：',arr[1:-1:2])  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 495,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [  5 101]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[5:1:-2])  #步长为负数时，开始下标必须大于结束下标\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 多维数组的索引访问"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 530,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为： [[ 1  2  3  4  5]\n",
      " [ 4  5  6  7  8]\n",
      " [ 7  8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "arr = np.array([[1, 2, 3, 4, 5],[4, 5, 6, 7, 8], [7, 8, 9, 10, 11]])\n",
    "print('创建的二维数组为：',arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 531,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [4 5]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[0,3:5])  #索引第0行中第3和4列的元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 532,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [[ 6  7  8]\n",
      " [ 9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "#索引第2和3行中第3～5列的元素\n",
    "print('索引结果为：',arr[1:,2:])  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 533,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [3 6 9]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[:,2])  #索引第2列的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 534,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [ 2  6 10]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\weihuimei\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:2: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "#从两个序列的对应位置取出两个整数来组成下标：arr[0,1], arr[1,2], arr[2,3]\n",
    "print('索引结果为：',arr[[(0,1,2),(1,2,3)]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 535,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [[ 4  6  7]\n",
      " [ 7  9 10]]\n"
     ]
    }
   ],
   "source": [
    "print('索引结果为：',arr[1:,(0,2,3)])  #索引第2、3行中第0、2、3列的元素\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 536,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "索引结果为： [3 9]\n"
     ]
    }
   ],
   "source": [
    "mask = np.array([1,0,1],dtype = np.bool)\n",
    "#mask是一个布尔数组，它索引第1、3行中第2列的元素\n",
    "print('索引结果为：',arr[mask,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.1.4 变换数组的形态"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 改变形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 505,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的一维数组为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(12)  #创建一维数组\n",
    "print('创建的一维数组为：',arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 506,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新的一维数组为： [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "print('新的一维数组为：',arr.reshape(3,4))  #设置数组的形状\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 507,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组维度为： 2\n"
     ]
    }
   ],
   "source": [
    "print('数组维度为：',arr.reshape(3,4).ndim)  #查看数组维度\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 展平"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 508,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "创建的二维数组为： [[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "数组展平后为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "arr = np.arange(12).reshape(3,4)\n",
    "print('创建的二维数组为：',arr)\n",
    "print('数组展平后为：',arr.ravel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 509,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组展平为： [ 0  1  2  3  4  5  6  7  8  9 10 11]\n"
     ]
    }
   ],
   "source": [
    "print('数组展平为：',arr.flatten())  #横向展平"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 510,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组展平为： [ 0  4  8  1  5  9  2  6 10  3  7 11]\n"
     ]
    }
   ],
   "source": [
    "print('数组展平为：',arr.flatten('F'))  #纵向展平"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 539,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n",
      "[[11 12 13 14]\n",
      " [15 16 17 18]\n",
      " [19 20 21 22]]\n"
     ]
    }
   ],
   "source": [
    "arr1=np.arange(0,12).reshape(3,4)\n",
    "print(arr1)\n",
    "arr2=np.arange(11,23).reshape(3,4)\n",
    "print(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 540,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3, 11, 12, 13, 14],\n",
       "       [ 4,  5,  6,  7, 15, 16, 17, 18],\n",
       "       [ 8,  9, 10, 11, 19, 20, 21, 22]])"
      ]
     },
     "execution_count": 540,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hstack((arr1,arr2)) # 使用hstack函数实现数组横向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 541,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [11, 12, 13, 14],\n",
       "       [15, 16, 17, 18],\n",
       "       [19, 20, 21, 22]])"
      ]
     },
     "execution_count": 541,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vstack((arr1,arr2)) # 使用vstack函数实现数组纵向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 543,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3, 11, 12, 13, 14],\n",
       "       [ 4,  5,  6,  7, 15, 16, 17, 18],\n",
       "       [ 8,  9, 10, 11, 19, 20, 21, 22]])"
      ]
     },
     "execution_count": 543,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((arr1,arr2),axis = 1)  #使用concatenate函数实现数组横向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 544,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [11, 12, 13, 14],\n",
       "       [15, 16, 17, 18],\n",
       "       [19, 20, 21, 22]])"
      ]
     },
     "execution_count": 544,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.concatenate((arr1,arr2),axis = 0)  #使用concatenate函数实现数组纵向组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 561,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11],\n",
       "       [11, 12, 13, 14],\n",
       "       [15, 16, 17, 18],\n",
       "       [19, 20, 21, 22]])"
      ]
     },
     "execution_count": 561,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.concatenate((arr1,arr2),axis = 0) \n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* `切割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 562,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1],\n",
       "        [ 4,  5],\n",
       "        [ 8,  9],\n",
       "        [11, 12],\n",
       "        [15, 16],\n",
       "        [19, 20]]), array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11],\n",
       "        [13, 14],\n",
       "        [17, 18],\n",
       "        [21, 22]])]"
      ]
     },
     "execution_count": 562,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(arr, 2) #使用hsplit函数实现数组横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 563,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]]), array([[11, 12, 13, 14],\n",
       "        [15, 16, 17, 18],\n",
       "        [19, 20, 21, 22]])]"
      ]
     },
     "execution_count": 563,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(arr, 2) #使用vsplit函数实现数组纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 564,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]]), array([[11, 12, 13, 14],\n",
       "        [15, 16, 17, 18],\n",
       "        [19, 20, 21, 22]])]"
      ]
     },
     "execution_count": 564,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(arr, 2) #使用vsplit函数实现数组纵向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 560,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[0, 1],\n",
       "        [4, 5],\n",
       "        [8, 9]]), array([[ 2,  3],\n",
       "        [ 6,  7],\n",
       "        [10, 11]])]"
      ]
     },
     "execution_count": 560,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(arr1, 2, axis=1)  #使用split函数实现数组横向分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 565,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0,  1,  2,  3],\n",
       "        [ 4,  5,  6,  7],\n",
       "        [ 8,  9, 10, 11]]), array([[11, 12, 13, 14],\n",
       "        [15, 16, 17, 18],\n",
       "        [19, 20, 21, 22]])]"
      ]
     },
     "execution_count": 565,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.split(arr, 2, axis=0)  #使用split函数实现数组纵向分割\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy专门用于python中的科学计算，能够高效操纵大型多维数组，可以当作通用数据的多维容器。\n",
    "\n",
    "可以快速处理数组，同时作为算法之间传递数据的容器,让其成为数据分析和机器学习最有用库之一。\n",
    "\n",
    "* 快速高效的多维数组对象ndarray\n",
    "\n",
    "* 用于对数组执行元素级计算以及直接对数组执行数组运算的函数对象 ufunc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2.1 Numpy 矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 矩阵的含义：一种特殊的数组，即维度为2的2维数组 ,列表、数组、矩阵的联系和区别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 323,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 323,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#list（列表）是Python六大内置数据类型之一，可以存放各种不同的数据类型\n",
    "lst = list((1,2,3))\n",
    "lst"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 486,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 598,
   "metadata": {},
   "outputs": [],
   "source": [
    "#ndarray（数组）是存储单一数据类型的多维数组\n",
    "a = np.array([1,2,3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 487,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 616,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3],\n",
       "        [4, 5, 6]])"
      ]
     },
     "execution_count": 616,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#matrix（矩阵）是一种特殊的数组，维度为2的数组\n",
    "m = np.matrix([[1,2,3],[4,5,6]])\n",
    "m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 488,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir(m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 575,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dir(np)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 矩阵的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 604,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]])"
      ]
     },
     "execution_count": 604,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 = np.mat(\"1 2 3;4 5 6;7 8 9\")  #使用mat函数创建矩阵\n",
    "matr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 613,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]])"
      ]
     },
     "execution_count": 613,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 = np.mat([[1, 2, 3],[4, 5 ,6],[7 ,8, 9]])  #使用mat函数创建矩阵\n",
    "matr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 606,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]])"
      ]
     },
     "execution_count": 606,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr2 = np.matrix('1 2 3;4 5 6;7 8 9')  #使用matrix函数创建矩阵\n",
    "matr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 607,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]])"
      ]
     },
     "execution_count": 607,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr2 = np.matrix([[1,2,3],[4,5,6],[7,8,9]])  #使用matrix函数创建矩阵\n",
    "matr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 608,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3, 1, 2, 3],\n",
       "        [4, 5, 6, 4, 5, 6],\n",
       "        [7, 8, 9, 7, 8, 9],\n",
       "        [1, 2, 3, 1, 2, 3],\n",
       "        [4, 5, 6, 4, 5, 6],\n",
       "        [7, 8, 9, 7, 8, 9]])"
      ]
     },
     "execution_count": 608,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat([[matr1, matr2], [matr2,matr1]] )  #使用bmat函数合成矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 611,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2, 3, 1, 2, 3],\n",
       "        [4, 5, 6, 4, 5, 6],\n",
       "        [7, 8, 9, 7, 8, 9],\n",
       "        [1, 2, 3, 1, 2, 3],\n",
       "        [4, 5, 6, 4, 5, 6],\n",
       "        [7, 8, 9, 7, 8, 9]])"
      ]
     },
     "execution_count": 611,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bmat('matr1, matr2;matr2,  matr1' )  #使用bmat函数合成矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 矩阵的运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 3,  6,  9],\n",
       "        [12, 15, 18],\n",
       "        [21, 24, 27]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1*3  #矩阵与数相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 2,  4,  6],\n",
       "        [ 8, 10, 12],\n",
       "        [14, 16, 18]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1+matr2  #矩阵相加减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 30,  36,  42],\n",
       "        [ 66,  81,  96],\n",
       "        [102, 126, 150]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1*matr2  # 矩阵相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1,  4,  9],\n",
       "        [16, 25, 36],\n",
       "        [49, 64, 81]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(matr1,matr2)  # 矩阵对应元素相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 数组和矩阵运算 对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 370,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 3,  6,  9],\n",
       "        [12, 15, 18],\n",
       "        [21, 24, 27]])"
      ]
     },
     "execution_count": 370,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1*3 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  6,  9],\n",
       "       [12, 15, 18],\n",
       "       [21, 24, 27]])"
      ]
     },
     "execution_count": 371,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(matr1) * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 372,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 2,  4,  6],\n",
       "        [ 8, 10, 12],\n",
       "        [14, 16, 18]])"
      ]
     },
     "execution_count": 372,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1+matr2  #矩阵相加减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 373,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [ 8, 10, 12],\n",
       "       [14, 16, 18]])"
      ]
     },
     "execution_count": 373,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(matr1)+np.asarray(matr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 374,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  6],\n",
       "       [ 8, 10, 12],\n",
       "       [14, 16, 18]])"
      ]
     },
     "execution_count": 374,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(matr1)+np.asarray(matr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 375,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 30,  36,  42],\n",
       "        [ 66,  81,  96],\n",
       "        [102, 126, 150]])"
      ]
     },
     "execution_count": 375,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1*matr2  # 矩阵与矩阵相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 376,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4,  9],\n",
       "       [16, 25, 36],\n",
       "       [49, 64, 81]])"
      ]
     },
     "execution_count": 376,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(matr1)*np.array(matr2)  # 数组与数组相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 377,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 30,  36,  42],\n",
       "       [ 66,  81,  96],\n",
       "       [102, 126, 150]])"
      ]
     },
     "execution_count": 377,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(np.array(matr1),np.array(matr2))  # 数组点积（内积）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 380,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[ 1,  4,  9],\n",
       "        [16, 25, 36],\n",
       "        [49, 64, 81]])"
      ]
     },
     "execution_count": 380,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(matr1,matr2)  # 矩阵对应元素相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 381,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4,  9],\n",
       "       [16, 25, 36],\n",
       "       [49, 64, 81]])"
      ]
     },
     "execution_count": 381,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.multiply(np.array(matr1),np.array(matr2))  # 矩阵对应元素相乘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 矩阵特有属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 320,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "(2, 2)\n"
     ]
    }
   ],
   "source": [
    "#矩阵是维度为2的特殊数组，是ndarray的子类，拥有ndarray的特性\n",
    "print(matr1.ndim)\n",
    "print(matr1.size)\n",
    "print(matr1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 382,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 矩阵特有的属性\n",
    "a1 = matr1.A  #返回自身数据的2维数组的一个视图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(numpy.matrix, numpy.ndarray)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(matr1),  type(a1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 4, 7],\n",
       "        [2, 5, 8],\n",
       "        [3, 6, 9]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1.T   # 返回自身的转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 321,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  4,  8],\n",
       "       [ 1,  5,  9],\n",
       "       [ 2,  6, 10],\n",
       "       [ 3,  7, 11]])"
      ]
     },
     "execution_count": 321,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object has no attribute 'H'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-59-82fccb36474e>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mH\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'H'"
     ]
    }
   ],
   "source": [
    "a1.H"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object has no attribute 'A'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-61-8c25f2742f55>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0ma1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mA\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'A'"
     ]
    }
   ],
   "source": [
    "a1.A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 4, 7],\n",
       "        [2, 5, 8],\n",
       "        [3, 6, 9]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1.H"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 矩阵和数组互转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 599,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "matrix([[1, 2],\n",
       "        [3, 4]])"
      ]
     },
     "execution_count": 599,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matr1 = np.asmatrix(np.array([[1,2],[3,4]]))\n",
    "matr1 = np.matrix(np.array([[1,2],[3,4]]))\n",
    "matr1 = np.mat(np.array([[1,2],[3,4]]))\n",
    "matr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 600,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 600,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.asarray(matr1)\n",
    "arr = np.array(matr1)\n",
    "arr = matr1.A\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.2.2 numpy 的通用函数 ufunc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 全称通用函数（universal function），是一种能够对**数组**中所有元素进行操作的函数。\n",
    "\n",
    "官网：\n",
    "https://numpy.org/devdocs/user/quickstart.html#universal-functions\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "四则运算：加（+）、减（-）、乘（*）、除（/）、幂（**）。数组间的四则运算表示对每个数组中的元素分别进行四则运算，所以形状必须相同。\n",
    "\n",
    "比较运算：>、<、==、>=、<=、!=。比较运算返回的结果是一个布尔数组，每个元素为每个数组对应元素的比较结果。\n",
    "\n",
    "逻辑运算：np.any函数表示逻辑“or”，np.all函数表示逻辑“and”。运算结果返回布尔值。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 257,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "can only concatenate list (not \"int\") to list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-257-5eae390205e5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mlst\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mlst\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mlst\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: can only concatenate list (not \"int\") to list"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3]\n",
    "lst * 2\n",
    "lst+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 4, 5]"
      ]
     },
     "execution_count": 258,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[i+2 for i in lst]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 4, 5])"
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array(lst)\n",
    "arr + 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [ 8,  9, 10, 11]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.arange(0,12).reshape(3,4)\n",
    "a1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 12,  8, 14],\n",
       "       [ 6, 16, 18, 14],\n",
       "       [16,  7, 17, 16]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a2 = np.random.randint(5,20,size=(3,4))\n",
    "a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 13, 10, 17],\n",
       "       [10, 21, 24, 21],\n",
       "       [24, 16, 27, 27]])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 + a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True,  True,  True],\n",
       "       [ True,  True,  True,  True],\n",
       "       [ True, False,  True,  True]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 < a2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.any(a1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* ufunc函数的广播机制\n",
    "\n",
    "广播（broadcasting）是指不同形状的数组之间执行算术运算的方式。需要遵循4个原则。\n",
    "\n",
    "1、让所有输入数组都向其中shape最长的数组看齐，shape中不足的部分都通过在前面加1补齐。\n",
    "\n",
    "2、输出数组的shape是输入数组shape的各个轴上的最大值。\n",
    "\n",
    "3、如果输入数组的某个轴和输出数组的对应轴的长度相同或者其长度为1时，这个数组能够用来计算，否则出错。\n",
    "\n",
    "4、当输入数组的某个轴的长度为1时，沿着此轴运算时都用此轴上的第一组值。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 一维数组的广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 217,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 217,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[0]*3,[1]*3,[2]*3,[3]*3])\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3])"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.array([1,2,3])\n",
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [2, 3, 4],\n",
       "       [3, 4, 5],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a+b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "#二维数组的广播机制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = np.array([[1],[2],[3],[4]])\n",
    "c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.3.1 numpy 文件存取"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 二进制的文件读写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.save(\"./tmp/save_arr\",np.array(matr1))  # save函数是以二进制的格式保存数据,默认扩展名  .npy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 134,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.load(\"./tmp/save_arr.npy\")   #load函数是从二进制的文件中读取数据,存储时可以省略扩展名，但读取时不能省略扩展名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savez('./tmp/savez_arr',matr1,matr2)  # savez函数可以将多个数组保存到一个文件中 ,默认扩展名  .npz\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "metadata": {},
   "outputs": [],
   "source": [
    "f =  np.load(\"./tmp/savez_arr.npz\")    #load函数是从二进制的文件中读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]]), array([[1, 2, 3],\n",
       "        [4, 5, 6],\n",
       "        [7, 8, 9]]))"
      ]
     },
     "execution_count": 221,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f['arr_0'],f['arr_1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['arr_0', 'arr_1']"
      ]
     },
     "execution_count": 222,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f.files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 读取文本格式的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.01405448,  0.53496932, -0.38699516,  1.64867793],\n",
       "       [-0.00432671,  0.22829534, -0.36239972, -0.63323347],\n",
       "       [-0.7985349 ,  0.10559589,  0.96142384, -0.94456555]])"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randn(3,4)\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(\"./tmp/arr1.txt\", arr1, fmt=\"%.6f\", delimiter=' ',encoding='utf-8')  #savetxt函数是将数组写到某种分隔符隔开的文本文件中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.014054,  0.534969, -0.386995,  1.648678],\n",
       "       [-0.004327,  0.228295, -0.3624  , -0.633233],\n",
       "       [-0.798535,  0.105596,  0.961424, -0.944566]])"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.loadtxt(\"./tmp/arr1.txt\",delimiter=\" \")  #loadtxt函数执行的是把文件加载到一个二维数组中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.014054,  0.534969, -0.386995,  1.648678],\n",
       "       [-0.004327,  0.228295, -0.3624  , -0.633233],\n",
       "       [-0.798535,  0.105596,  0.961424, -0.944566]])"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.genfromtxt(\"./tmp/arr1.txt\", delimiter = \" \")  #genfromtxt函数面向的是结构化数组和缺失数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.3.2 运用 numpy 进行统计分析"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 直接排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 617,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 7,  2, 10, 17, 17],\n",
       "       [ 8, 13, 10,  3,  4],\n",
       "       [18, 11,  4, 19,  2],\n",
       "       [10,  6,  7, 10,  5]])"
      ]
     },
     "execution_count": 617,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(1,20,size=(4,5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 621,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  7, 10, 10],\n",
       "       [ 2,  4,  8, 10, 13],\n",
       "       [ 3,  6, 10, 17, 17],\n",
       "       [ 5,  7, 11, 18, 19]])"
      ]
     },
     "execution_count": 621,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sort()\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 619,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  7, 10, 17, 17],\n",
       "       [ 3,  4,  8, 10, 13],\n",
       "       [ 2,  4, 11, 18, 19],\n",
       "       [ 5,  6,  7, 10, 10]])"
      ]
     },
     "execution_count": 619,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sort(axis = 1)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 620,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2,  4,  7, 10, 10],\n",
       "       [ 2,  4,  8, 10, 13],\n",
       "       [ 3,  6, 10, 17, 17],\n",
       "       [ 5,  7, 11, 18, 19]])"
      ]
     },
     "execution_count": 620,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.sort(axis = 0)\n",
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 间接排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 361,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  6, 14],\n",
       "       [14, 13,  3],\n",
       "       [18, 15,  4],\n",
       "       [10,  8,  2]])"
      ]
     },
     "execution_count": 361,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seed = np.random.seed(30)\n",
    "arr = np.random.randint(1,20,size=(4,3))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 362,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 3],\n",
       "       [3, 3, 1],\n",
       "       [1, 1, 2],\n",
       "       [2, 2, 0]], dtype=int64)"
      ]
     },
     "execution_count": 362,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.argsort(axis = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 363,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 0, 4, 6, 5, 3, 1], dtype=int64)"
      ]
     },
     "execution_count": 363,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,5,1,4,3,4,4]  # First column\n",
    "b = [9,4,0,4,0,2,1]  # Second column\n",
    "ind = np.lexsort((b,a)) # Sort by a, then by b\n",
    "ind"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 去重  -- unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 568,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 3, 2, 4],\n",
       "       [3, 7, 5, 7],\n",
       "       [5, 9, 7, 3]])"
      ]
     },
     "execution_count": 568,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(1,10,size=(3,4))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 569,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 7, 9])"
      ]
     },
     "execution_count": 569,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 570,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 3, 2, 4],\n",
       "       [3, 7, 5, 7],\n",
       "       [5, 9, 7, 3]])"
      ]
     },
     "execution_count": 570,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 重复  -- tile,repeat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 571,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 3, 2, 4, 9, 3, 2, 4],\n",
       "       [3, 7, 5, 7, 3, 7, 5, 7],\n",
       "       [5, 9, 7, 3, 5, 9, 7, 3]])"
      ]
     },
     "execution_count": 571,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.tile(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 572,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([9, 9, 3, 3, 2, 2, 4, 4, 3, 3, 7, 7, 5, 5, 7, 7, 5, 5, 9, 9, 7, 7,\n",
       "       3, 3])"
      ]
     },
     "execution_count": 572,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.repeat(arr,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 573,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 7, 9])"
      ]
     },
     "execution_count": 573,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.unique(arr)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 其他常用统计函数\n",
    "\n",
    "当axis=0时，表示沿着纵轴计算。当axis=1时，表示沿着横轴计算。默认时计算一个总值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sum  计算数组的和\n",
    "# mean  计算数组均值\n",
    "# std  计算数组标准差\n",
    "# var  计算数组方差\n",
    "# min  计算数组最小值\n",
    "# max  计算数组最大值\n",
    "# argmin   返回数组最小元素索引\n",
    "# argmax   返回数组最大元素索引\n",
    "# cumsum   计算所有元素的累计和\n",
    "# cumprod  计算所有元素的累计积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 6, 1, 7, 7],\n",
       "       [2, 1, 8, 2, 6],\n",
       "       [8, 2, 6, 4, 1],\n",
       "       [9, 3, 7, 4, 9]])"
      ]
     },
     "execution_count": 233,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(1,10,size=(4,5))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "97\n",
      "4.85\n",
      "2.706935536727833\n",
      "7.327500000000001\n",
      "1\n",
      "9\n",
      "2\n",
      "15\n",
      "[ 4 10 11 18 25 27 28 36 38 44 52 54 60 64 65 74 77 84 88 97]\n",
      "[          4          24          24         168        1176        2352\n",
      "        2352       18816       37632      225792     1806336     3612672\n",
      "    21676032    86704128    86704128   780337152 -1953955840  -792788992\n",
      "  1123811328  1524367360]\n"
     ]
    }
   ],
   "source": [
    "print(np.sum(arr))# sum  计算数组的和\n",
    "print(np.mean(arr))# mean  计算数组均值\n",
    "print(np.std(arr))# std  计算数组标准差\n",
    "print(np.var(arr))# var  计算数组方差\n",
    "print(np.min(arr))# min  计算数组最小值\n",
    "print(np.max(arr))# max  计算数组最大值\n",
    "print(np.argmin(arr))# argmin   返回数组最小元素索引\n",
    "print(np.argmax(arr))# argmax   返回数组最大元素索引\n",
    "print(np.cumsum(arr))# cumsum   计算所有元素的累计和\n",
    "print(np.cumprod(arr))# cumprod  计算所有元素的累计积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 359,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n",
      "2.0\n",
      "0.816496580927726\n",
      "0.6666666666666666\n",
      "1\n",
      "3\n",
      "0\n",
      "2\n",
      "[1 3 6]\n",
      "[1 2 6]\n"
     ]
    }
   ],
   "source": [
    "lst = [1,2,3]\n",
    "print(np.sum(lst))# sum  计算数组的和\n",
    "print(np.mean(lst))# mean  计算数组均值\n",
    "print(np.std(lst))# std  计算数组标准差\n",
    "print(np.var(lst))# var  计算数组方差\n",
    "print(np.min(lst))# min  计算数组最小值\n",
    "print(np.max(lst))# max  计算数组最大值\n",
    "print(np.argmin(lst))# argmin   返回数组最小元素索引\n",
    "print(np.argmax(lst))# argmax   返回数组最大元素索引\n",
    "print(np.cumsum(lst))# cumsum   计算所有元素的累计和\n",
    "print(np.cumprod(lst))# cumprod  计算所有元素的累计积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
